// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   check_delete_ok.java

package manufacture.apply;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import validata.ValidataNumber;
import validata.ValidataTag;

public class check_delete_ok extends HttpServlet
{

	public check_delete_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		try
		{
			HttpSession dbSession = request.getSession();
			javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
			nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 manufacture_db1 = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
			ValidataNumber validata = new ValidataNumber();
			ValidataTag vt = new ValidataTag();
			counter count = new counter(dbApplication);
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && manufacture_db1.conn((String)dbSession.getAttribute("unit_db_name")) && stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String config_id = request.getParameter("config_id");
				String apply_ID = request.getParameter("apply_ID");
				String product_ID = request.getParameter("product_ID");
				String choice = request.getParameter("choice");
				String checker_ID = request.getParameter("checker_ID");
				String checker = request.getParameter("checker");
				String check_time = request.getParameter("check_time");
				String designer = request.getParameter("designer");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyb);
				String sql6 = (new StringBuilder("select id from manufacture_workflow where object_ID='")).append(apply_ID).append("' and ((check_tag='0' and config_id<'").append(config_id).append("') or (check_tag='1' and config_id='").append(config_id).append("'))").toString();
				ResultSet rs6 = manufacture_db.executeQuery(sql6);
				if (!rs6.next())
				{
					if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "manufacture_apply", "apply_ID", apply_ID, "check_tag").equals("0"))
					{
						if (choice != null)
						{
							if (choice.equals(""))
							{
								String sql = (new StringBuilder("update manufacture_apply set designer='")).append(designer).append("',register='").append(register).append("',register_time='").append(register_time).append("',remark='").append(remark).append("',checker='").append(checker).append("',check_time='").append(check_time).append("',check_tag='9' where apply_ID='").append(apply_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
								String sql1 = (new StringBuilder("select * from manufacture_apply where apply_ID='")).append(apply_ID).append("'").toString();
								for (ResultSet rs1 = manufacture_db.executeQuery(sql1); rs1.next();)
								{
									for (StringTokenizer tokenTO = new StringTokenizer(rs1.getString("pay_ID_group"), ", "); tokenTO.hasMoreTokens();)
									{
										String sql2 = (new StringBuilder("select * from stock_pay where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();
										ResultSet rs2 = stock_db.executeQuery(sql2);
										if (rs2.next())
										{
											String sql3 = (new StringBuilder("update crm_order set manufacture_tag='1' where order_ID='")).append(rs2.getString("reasonexact")).append("'").toString();
											manufacture_db1.executeUpdate(sql3);
											String sql4 = (new StringBuilder("update stock_pay set manufacture_apply_tag='0' where pay_ID='")).append(rs2.getString("pay_ID")).append("'").toString();
											stock_db.executeUpdate(sql4);
										}
									}

								}

								stock_db.commit();
								sql = (new StringBuilder("delete from manufacture_workflow where object_ID='")).append(apply_ID).append("'").toString();
								manufacture_db.executeUpdate(sql);
							} else
							{
								sql6 = (new StringBuilder("select id from manufacture_workflow where object_ID='")).append(apply_ID).append("' and config_id<'").append(config_id).append("' and config_id>='").append(choice).append("'").toString();
								String sql;
								for (rs6 = manufacture_db.executeQuery(sql6); rs6.next(); manufacture_db1.executeUpdate(sql))
									sql = (new StringBuilder("update manufacture_workflow set check_tag='0' where id='")).append(rs6.getString("id")).append("'").toString();

							}
							response.sendRedirect("manufacture/apply/check_delete_ok.jsp?finished_tag=0");
						} else
						{
							response.sendRedirect("manufacture/apply/check_delete_ok.jsp?finished_tag=1");
						}
					} else
					{
						response.sendRedirect("manufacture/apply/check_delete_ok.jsp?finished_tag=3");
					}
				} else
				{
					response.sendRedirect("manufacture/apply/check_delete_ok.jsp?finished_tag=2");
				}
				manufacture_db.commit();
				manufacture_db1.commit();
				manufacture_db.close();
				manufacture_db1.close();
				stock_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
